package com.liuh.fram.log;

import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Priority;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.spi.LoggingEvent;

import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.liuh.fram.SendMail;
import com.liuh.fram.util.DateKitExt;

/**
 * @note 有异常发邮件
 * @author liuh
 * */
public class Log4jLogExtra extends Log {

	private org.apache.log4j.Logger log;
	private static final String callerFQCN = Log4jLogExtra.class.getName();

	public Log4jLogExtra(Class<?> clazz) {
		log = org.apache.log4j.Logger.getLogger(clazz);
	}

	public Log4jLogExtra(String name) {
		log = org.apache.log4j.Logger.getLogger(name);
	}

	public void info(String message) {
		log.log(callerFQCN, Level.INFO, message, null);
	}

	public void info(String message, Throwable t) {
		log.log(callerFQCN, Level.INFO, message, t);
	}

	public void debug(String message) {
		log.log(callerFQCN, Level.DEBUG, message, null);
	}

	public void debug(String message, Throwable t) {
		log.log(callerFQCN, Level.DEBUG, message, t);
	}

	public void warn(String message) {
		log.log(callerFQCN, Level.WARN, message, null);
	}

	public void warn(String message, Throwable t) {
		log.log(callerFQCN, Level.WARN, message, t);
	}

	public void error(String message) {
		log.log(callerFQCN, Level.ERROR, message, null);
	}

	public void error(String message, Throwable t) {
		log.log(callerFQCN, Level.ERROR, message, t);
		// 发送邮件
		isSend_ErrorMail(callerFQCN, Level.ERROR, message, t);
	}

	public void fatal(String message) {
		log.log(callerFQCN, Level.FATAL, message, null);
	}

	public void fatal(String message, Throwable t) {
		log.log(callerFQCN, Level.FATAL, message, t);
		// 发送邮件
		isSend_ErrorMail(callerFQCN, Level.FATAL, message, t);
	}

	public boolean isDebugEnabled() {
		return log.isDebugEnabled();
	}

	public boolean isInfoEnabled() {
		return log.isInfoEnabled();
	}

	public boolean isWarnEnabled() {
		return log.isEnabledFor(Level.WARN);
	}

	public boolean isErrorEnabled() {
		return log.isEnabledFor(Level.ERROR);
	}

	public boolean isFatalEnabled() {
		return log.isEnabledFor(Level.FATAL);
	}

	/**
	 * @note 是否发送异常邮件
	 * @author liuh
	 * */
	private void isSend_ErrorMail(String fqcn, Priority level, Object message,
			Throwable t) {
		boolean isSendMail = PropKit.getBoolean("isSendMail");
		if (isSendMail) {
			String mess = forcedLog(fqcn, level, message, t);
			// 发送异常邮件
			new SendMail().synSendHtmlMail(PropKit.get("email_account"),
					PropKit.get("email_topic"), mess);
		}
	}

	/**
	 * @note 构造异常主体
	 * @author liuh
	 * */
	String forcedLog(String fqcn, Priority level, Object message, Throwable t) {
		LoggingEvent event = new LoggingEvent(fqcn, log, level, message, t);
		StringBuffer sbuf = new StringBuffer();
		sbuf.append("操作时间：" + DateKitExt.getCurrentDateTime());
		sbuf.append("</p>");
		sbuf.append("级别：" + level);
		sbuf.append("</p>");
		sbuf.append("错误信息：" + event.getRenderedMessage());
		sbuf.append("</p>");
		sbuf.append("--------------------------------------------------</p>");
		appendThrowableAsHTML(event.getThrowableStrRep(), sbuf);
		return sbuf.toString();
	}

	private void appendThrowableAsHTML(String[] s, StringBuffer sbuf) {
		if (s != null) {
			int len = s.length;
			if (len == 0)
				return;
			sbuf.append(Transform.escapeTags(s[0]));
			sbuf.append(Layout.LINE_SEP);
			for (int i = 1; i < len; i++) {
				sbuf.append("<br>&nbsp;&nbsp;&nbsp;&nbsp;");
				sbuf.append(Transform.escapeTags(s[i]));
				sbuf.append(Layout.LINE_SEP);
			}
		}
	}

}
